package com.xq.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.cache.EhCache;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.xq.controller.AuthenticationInterceptor;
import com.xq.controller.CommentController;
import com.xq.controller.ExceptionInterceptor;
import com.xq.controller.ShopController;
import com.xq.controller.TopicController;
import com.xq.controller.UserController;
import com.xq.controller.XiaoQuController;
import com.xq.model.Comment;
import com.xq.model.LoginToken;
import com.xq.model.Shop;
import com.xq.model.Topic;
import com.xq.model.User;
import com.xq.model.XiaoQu;

public class AppConfig extends JFinalConfig
{

    @Override
    public void configConstant(Constants me)
    {
        loadPropertyFile("config.txt");
        me.setDevMode(getPropertyToBoolean("devMode", false));
    }

    @Override
    public void configRoute(Routes me)
    {
        me.add("/user", UserController.class);
        me.add("/xiaoqu", XiaoQuController.class);
        me.add("/topic", TopicController.class);
        me.add("/shop", ShopController.class);
        me.add("/comment", CommentController.class);
    }

    @Override
    public void configPlugin(Plugins me)
    {
        // spring
        // me.add(new SpringPlugin());
        // c3p0
        C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),
                getProperty("user"), getProperty("password"));
        me.add(c3p0Plugin);
        // active record
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        me.add(arp);
        // set ehcache
        arp.setCache(new EhCache());

        // mapping objects
        arp.addMapping(User.TABLE_NAME, User.class);
        arp.addMapping(XiaoQu.TABLE_NAME, XiaoQu.class);
        arp.addMapping(LoginToken.TABLE_NAME, LoginToken.class);
        arp.addMapping(Topic.TABLE_NAME, Topic.class);
        arp.addMapping(Shop.TABLE_NAME, Shop.class);
        arp.addMapping(Comment.TABLE_NAME, Comment.class);
        // arp.addMapping(RoleModel.TABLE_NAME, RoleModel.class);
        // arp.addMapping(ModuleModel.TABLE_NAME, ModuleModel.class);
        // arp.addMapping(ProductModel.TABLE_NAME, ProductModel.class);
        // arp.addMapping(AuthorizerModel.TABLE_NAME, AuthorizerModel.class);
        // arp.addMapping(ClientModel.TABLE_NAME, ClientModel.class);
        // arp.addMapping(DriverModel.TABLE_NAME, DriverModel.class);
        // arp.addMapping(VehicleModel.TABLE_NAME, VehicleModel.class);
        // arp.addMapping(VcostModel.TABLE_NAME, VcostModel.class);
        // arp.addMapping(CarrierModel.TABLE_NAME, CarrierModel.class);
        // arp.addMapping(OilcardModel.TABLE_NAME, OilcardModel.class);
        // arp.addMapping(PayHistoryModel.TABLE_NAME, PayHistoryModel.class);
        // arp.addMapping(RechargeHistoryModel.TABLE_NAME,
        // RechargeHistoryModel.class);
        // arp.addMapping(FixHistoryModel.TABLE_NAME, FixHistoryModel.class);
        // arp.addMapping(Innercity.TABLE_NAME, Innercity.class);
        // arp.addMapping(InnercityOp.TABLE_NAME, InnercityOp.class);
        // arp.addMapping(InnercityEd.TABLE_NAME, InnercityEd.class);
    }

    @Override
    public void configInterceptor(Interceptors me)
    {
    	me.add(new ExceptionInterceptor());
    	me.add(new AuthenticationInterceptor());
    }

    @Override
    public void configHandler(Handlers me)
    {

    }

    public static void main(String[] args)
    {
        JFinal.start("WebRoot", 8081, "/", 5);
    }
}
